#include "cmd_sell.h"


cmd_Request::cmd_Request(){
}

cmd_Request::cmd_Request(Request * request):request(request){
}

string cmd_Request::query(){
    stringstream ss;
    switch(cmd){
        case CADASTRAR:
            ss << "INSERT INTO tb_sales VALUES("
                << "" << request->getQuantity().get() << ","
                << "'" << request->getIdentifier().get() << "',"
                << "'" << request->getCPF().get() << "',"
                << "'" << request->getAlbum().get() << "' ,"
                << "" << request->getStatus().get() << ""
            << ");";
            break;

        case ALTERAR_STATUS:
            ss << "UPDATE tb_sales SET status = '1'   WHERE "
                << "identifier = '" << request->getIdentifier().get() << "'"
            << ";";
            break;

        case PESQUISAR:
        default:
            ss << "SELECT tb_sales.* FROM tb_sales "
            << " INNER JOIN tb_albuns"
                    <<" ON tb_sales.id_album = tb_albuns.identifier"
                << " WHERE tb_albuns.cpf = '" << request->getCPF().get() <<"'"
                    << " AND tb_sales.identifier = '"  << request->getIdentifier().get() << "'"
            << ";";
            break;

        case PESQUISAR_ALTERAR:
            ss << "SELECT tb_sales.status "
                <<" FROM tb_sales "
                << " INNER JOIN tb_albuns"
                    <<" ON tb_sales.id_album = tb_albuns.identifier"
                << " WHERE tb_albuns.cpf = '" << request->getCPF().get() <<"'"
                    << " AND tb_sales.identifier = '"  << request->getIdentifier().get() << "'"
               << ";" ;

        break;
        case SELECTALBUM:
            ss << "SELECT * FROM tb_albuns WHERE "
                << "identifier = '" << album->getIdentifier().get() << "' "
                << " AND access_level = '1' "
            << ";";
            break;

        case CHECKPENDENCIAS:

                ss<< "SELECT status FROM tb_sales WHERE "
                    << "status = '0' "
                    << " AND cpf = '" << request->getCPF().get() <<"'"
                << ";";
            break;

            case CRIABANCO:

                 /*
                ss << "DROP TABLE tb_sales; "
                    << "CREATE TABLE tb_sales (quantity INT, identifier VARCHAR(10) , cpf VARCHAR (11) REFERENCES tb_users (cpf), id_album VARCHAR (10) REFERENCES tb_albuns (identifier), status int);"
                ;
                   ss << "ALTER TABLE tb_sales ADD COLUMN status int;" ;

                ss<< "BEGIN TRANSACTION;"
                  << "CREATE TABLE tb_photos (identifier VARCHAR (10) NOT NULL PRIMARY KEY, data VARCHAR (10),speed FLOAT, openness FLOAT, iso INT);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto1','20/06/2011',0.25,1.0,80);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto2','10/05/2001',0.25,1.0,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto3','10/10/2010',1.0,1.0,200);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto4','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto5','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto6','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto7','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto8','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto9','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('Foto0','22/11/2001',0.1,0.1,100);"
                  << "INSERT INTO \"tb_photos\" VALUES('foto1','01/01/2010',1.0,1.0,80);"
                  << "CREATE TABLE tb_users (name VARCHAR (20), password VARCHAR (5) NOT NULL, address VARCHAR (30), cpf VARCHAR (11) PRIMARY KEY, total_sales FLOAT);"
                  << "INSERT INTO \"tb_users\" VALUES('Tercio','12345','End, 1','12345678900',0.0);"
                  << "INSERT INTO \"tb_users\" VALUES('Roberto','12345','End. 1','12345678901',0.0);"
                  << "INSERT INTO \"tb_users\" VALUES('Caio','12345','End. 1','12345678902',0.0);"
                  << "CREATE TABLE tb_albuns (identifier VARCHAR (10) PRIMARY KEY NOT NULL, description VARCHAR (50), access_level BOOLEAN, price FLOAT, cpf VARCHAR (11) REFERENCES tb_users (cpf));"
                  << "INSERT INTO \"tb_albuns\" VALUES('Album1','Ferias','true',10.0,'12345678900');"
                  << "INSERT INTO \"tb_albuns\" VALUES('Album2','Lazer','true',10.0,'12345678901');"
                  << "INSERT INTO \"tb_albuns\" VALUES('Album3','Estudos','false',20.0,'12345678902');"
                  << "INSERT INTO \"tb_albuns\" VALUES('album1','album',0,15.0,'cpf_usuario');"
                  << "CREATE TABLE tb_sales (quantity INT, identifier VARCHAR(10) , cpf VARCHAR (11) REFERENCES tb_users (cpf), id_album VARCHAR (10) REFERENCES tb_albuns (identifier), status int);"
                  << "DELETE FROM sqlite_sequence;"
                  << "CREATE TABLE tb_photosalbum (id_photo VARCHAR (10) REFERENCES tb_photos (identifier), id_album VARCHAR (10) REFERENCES tb_albuns (identifier), PRIMARY KEY (id_photo, id_album));"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto1','Album1');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto2','Album1');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto3','Album1');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto3','Album2');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto4','Album2');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto5','Album2');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto6','Album3');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto7','Album3');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto8','Album3');"
                  << "INSERT INTO \"tb_photosalbum\" VALUES('Foto1','Album3');"
                  << "COMMIT;" ;

                  */
            break;
    }
    return ss.str();
}

void cmd_Request::setRequest(Request * request){
    this->request = request;
}

Request * cmd_Request::getRequest(){
    return request;
}

void cmd_Request::setCmd(int cmd){
    this->cmd = cmd;
}
void cmd_Request::setAlbum(Album * album){
    this->album = album;
}

Album * cmd_Request::getAlbum(){
    return album;
}
